package com.portal.cwegportal.mapper;

import com.portal.cwegportal.input.meeting.GetMettingByTimeUserInput;
import com.portal.cwegportal.input.meeting.PageLoadMeetingInput;
import com.portal.cwegportal.model.Meeting;
import com.portal.cwegportal.output.meeting.GetMettingByTimeUserOutput;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface MeetingMapper extends Mapper<Meeting> {
    @Results({
            @Result(column = "{meetingID=id}",property = "meetingNoticeList", javaType=List.class, many=@Many(
                    select="com.portal.cwegportal.mapper.MeetingNoticeMapper.getMeetingNoticeByMeetingID"
            )),
    })
    @Select("<script>" +
            "select DISTINCT meeting.* from  meeting left join meeting_notice on meeting_notice.meeting_id=meeting.id " +
            "where meeting.gmt_status=1 and meeting_notice.user_code=#{currentUserCode} and meeting.is_deleted=0  and meeting.title like concat ('%',#{title},'%') " +
            "<when test='endTime!=null'>   and <![CDATA[ end_time <= #{endTime,jdbcType=DATE} ]]>   </when>" +
            "<when test='startTime!=null'> and <![CDATA[ start_time >= #{startTime,jdbcType=DATE} ]]> </when>" +
            " ORDER BY meeting.gms_status desc,meeting.gmt_create desc " +
            "</script>"
    )

    public List<Meeting> getMeetingByNotesUserCode(PageLoadMeetingInput input);

    @Results({
            @Result(column = "{meetingID=id}",property = "meetingNoticeList", javaType=List.class, many=@Many(
                    select="com.portal.cwegportal.mapper.MeetingNoticeMapper.getMeetingNoticeByMeetingID"
            )),
                })
    @Select("<script>" +
            "select * from meeting where creator_code=#{currentUserCode}" +
            "and is_deleted=0  " +
            "and title like concat ('%',#{title},'%') " +
            "<when test='endTime!=null'>   and <![CDATA[ end_time <= #{endTime,jdbcType=DATE} ]]>   </when>" +
            "<when test='startTime!=null'> and <![CDATA[ start_time >= #{startTime,jdbcType=DATE} ]]> </when>" +
            " ORDER BY gms_status desc,gmt_create desc " +
            "</script>"
    )
    public List<Meeting> getMeetingByCurrentUserCode(PageLoadMeetingInput input);

    @Select("<script>" +
            "SELECT date(start_time) as meetData, count(1) as meetNum from meeting m "+
            "inner join meeting_notice n on m.id=n.meeting_id where m.is_deleted=0 and m.gmt_status=1 and "+
            " (m.host_code=#{userCode} or n.user_code=#{userCode} or m.creator_code=#{userCode}) and "+
             " Year(start_time)=#{year} and Month(start_time)=#{month} GROUP BY date(start_time) "+
            "</script>"
    )
    public List<GetMettingByTimeUserOutput> getMeetingByCurrentUserTime(GetMettingByTimeUserInput input);


}

